package com.optitronics.beans;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.persistence.TableGenerator;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;

import com.optitronics.util.OptitronicsConstants;

@Entity
@Table(name = "email_messages")
@Configurable
public class EmailMessage {

	@PersistenceContext
	transient EntityManager entityManager;

	@Id
	@TableGenerator(name = "mailsGenerator", pkColumnValue = "EMAIL_MESSAGES", table = OptitronicsConstants.COUNTER_TABLE_NAME, pkColumnName = OptitronicsConstants.COUNTER_PK_COLUMN_NAME, valueColumnName = OptitronicsConstants.COUNTER_VALUE_COLUMN_NAME, allocationSize = OptitronicsConstants.COUNTER_ALLOCATION_SIZE)
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "mailsGenerator")
	@Column(name = "ID")
	private Integer id;

	@Column(name = "FROM_ADDRESS")
	private String fromAddress;

	@Column(name = "SUBJECT")
	private String subject;

	@Column(name = "TO_ADDRESS")
	private String toAddress;

	@Column(name = "CONTENT")
	private String content;

	@Column(name = "SENDING_DATE")
	private Date sendingDate;

	@Column(name = "ATTACHMENT_LOCATION")
	private String attachmentLocation;

	@Transactional
	public void persist() {
		if (this.entityManager == null)
			this.entityManager = entityManager();
		this.entityManager.persist(this);
	}

	@Transactional
	public void remove() {
		if (this.entityManager == null)
			this.entityManager = entityManager();
		if (this.entityManager.contains(this)) {
			this.entityManager.remove(this);
		} else {
			EmailMessage attached = this.entityManager.find(this.getClass(), this.id);
			this.entityManager.remove(attached);
		}
	}

	@Transactional
	public void flush() {
		if (this.entityManager == null)
			this.entityManager = entityManager();
		this.entityManager.flush();
	}

	public static final EntityManager entityManager() {
		EntityManager em = new EmailMessage().entityManager;
		if (em == null)
			throw new IllegalStateException(
					"Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
		return em;
	}

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public static EmailMessage findEmailMessages(Integer id) {
		if (id == null)
			return null;
		return entityManager().find(EmailMessage.class, id);
	}

	public static List<EmailMessage> findEmailMessagesEntries(int firstResult, int maxResults) {
		return entityManager().createQuery("select o from EmailMessage o", EmailMessage.class)
				.setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
	}

	public static long countEmailMessageses() {
		return entityManager().createQuery("select count(o) from EmailMessage o", Long.class).getSingleResult();
	}

	public static List<EmailMessage> findAllEmailMessageses() {
		return entityManager().createQuery("select o from EmailMessage o", EmailMessage.class).getResultList();
	}

	public String getFromAddress() {
		return this.fromAddress;
	}

	public void setFromAddress(String fromAddress) {
		this.fromAddress = fromAddress;
	}

	public String getSubject() {
		return this.subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getToAddress() {
		return this.toAddress;
	}

	public void setToAddress(String toAddress) {
		this.toAddress = toAddress;
	}

	public String getContent() {
		return this.content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Date getSendingDate() {
		return sendingDate;
	}

	public void setSendingDate(Date sendingDate) {
		this.sendingDate = sendingDate;
	}

	public String getAttachmentLocation() {
		return attachmentLocation;
	}

	public void setAttachmentLocation(String attachmentLocation) {
		this.attachmentLocation = attachmentLocation;
	}
}
